package com.excuse.excusebuilder.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.adapter.CommunityFragmentAdapter;
import com.excuse.excusebuilder.adapter.CommunityItemDetailAdapter;
import com.excuse.excusebuilder.base.BaseActivity;
import com.excuse.excusebuilder.bean.Feedback;
import com.excuse.excusebuilder.bean.MessageLeft;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.bean.User;
import com.excuse.excusebuilder.commen.util.ToastUtil;
import com.excuse.excusebuilder.view.fragment.MineFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by U-nookia on 2017/5/3.
 */

public class MineDetailActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.mine_detail_recycler)
    RecyclerView mineDetailRecycler;
    @BindView(R.id.edit_feedback)
    EditText editFeedback;
    @BindView(R.id.bt_feedback)
    Button btFeedback;
    @BindView(R.id.feedback_view)
    LinearLayout feedbackView;

    private int item;

    public static void jumpToDetailActivity(Context context, Bundle bundle) {
        Intent intent = new Intent(context, MineDetailActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        item = intent.getIntExtra("item", 0);
        switch (item) {
            case MineFragment.POST:
                initPostData();
                break;
            case MineFragment.COMMENT:
                initCommentData();
                break;
            case MineFragment.FEEDBACK:
                initFeedbackView();
                break;
        }
    }

    private void initFeedbackView() {
        initToolbar(toolbar,"反馈错误");
        feedbackView.setVisibility(View.VISIBLE);
    }

    private void initCommentData() {
        initToolbar(toolbar, "收到的评论");
        BmobQuery<MessageLeft> queryMsg = new BmobQuery<>();
        queryMsg.addWhereEqualTo("messageTo", BmobUser.getCurrentUser());
        queryMsg.include("post,messageTo,messageFrom");
        queryMsg.order("-createdAt");
        queryMsg.setLimit(1000);
        queryMsg.findObjects(new FindListener<MessageLeft>() {
            @Override
            public void done(List<MessageLeft> list, BmobException e) {
                if (e != null) {
                    ToastUtil.makeShortToast(context, e.toString());
                    return;
                }
                showCommentsList(list);
            }
        });
    }

    private void showCommentsList(List<MessageLeft> list) {
        CommunityItemDetailAdapter adapter = new CommunityItemDetailAdapter(list);
        mineDetailRecycler.setLayoutManager(new LinearLayoutManager(context));
        mineDetailRecycler.setAdapter(adapter);
    }

    private void initPostData() {
        initToolbar(toolbar, "发布的帖子");
        BmobQuery<Post> queryPosts = new BmobQuery<>();
        User user = new User();
        user.setObjectId(BmobUser.getCurrentUser().getObjectId());
        queryPosts.addWhereEqualTo("user", new BmobPointer(user));
        queryPosts.include("user");
        queryPosts.order("-createdAt");
        queryPosts.setLimit(1000);
        queryPosts.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                if (e != null) {
                    ToastUtil.makeShortToast(context, e.toString());
                    return;
                }
                showList(list);
            }
        });
    }

    private void showList(List<Post> list) {
        CommunityFragmentAdapter adapter = new CommunityFragmentAdapter(list);
        mineDetailRecycler.setLayoutManager(new LinearLayoutManager(context));
        mineDetailRecycler.setAdapter(adapter);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_mine_detail;
    }

    @OnClick(R.id.bt_feedback)
    public void onViewClicked() {
        String feedback = editFeedback.getText().toString();
        if (feedback.equals("")){
            ToastUtil.makeShortToast(context,"输入不能为空");
            return;
        }
        Feedback content = new Feedback();
        content.setContent(feedback);
        content.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e!=null){
                    ToastUtil.makeShortToast(context,e.toString());
                    return;
                }
                ToastUtil.makeShortToast(context,"反馈成功");
                finish();
            }
        });
    }
}
